package com.qf.blog.commons.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class BlogExceptionHanlder {
    @ExceptionHandler (Exception.class)
    @ResponseBody
    public ResponseEntity systemException(Exception e) {
        // 记录日志
        log.error("系统异常", e);
        // 响应用户
        return ResponseEntity
                .status(HttpStatus.INTERNAL_SERVER_ERROR).body("系统异常,请联系管理员");
    }

    @ExceptionHandler (BusinessException.class)
    @ResponseBody
    public ResponseEntity blogException(BusinessException e) {
        log.error("业务异常", e);
        Integer code = Integer.valueOf(e.getCode().toString().substring(0, 3));
        return ResponseEntity
                .status(code).body(e.getMsg());
    }
}
